package com.usian.controller;

import com.usian.pojo.Etc;
import com.usian.pojo.User;
import com.usian.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttribute;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/toregister")
    public String register(){

        return "register";
    }

    @RequestMapping("/register")
    public String zhuce(User user){

        int a = userService.findByName(user.getName());

        if (a==0){
            userService.login(user);
            return "registersuccess";
        }else {
            return "registerfalse";
        }

    }

    @RequestMapping("/tologin")
    public String tologin(){
        return "login";
    }

    @RequestMapping("/login")
    public String login(User user, HttpServletRequest request){
        HttpSession session = request.getSession();
        session.setAttribute("username",user.getName());
        User user1 = userService.find(user);
        if (user1!=null){
            if (user1.getRoleId()==1){
                return "redirect:/user/findAdmin";
            }else {
                return "redirect:/user/findList";
            }
        }else {
            return "login";
        }
    }

    @RequestMapping("/findList")
    public String findList(Etc etc,HttpServletRequest request, Model model){
        HttpSession session = request.getSession();
        String username = (String)session.getAttribute("username");
        etc.setName(username);
        List<Etc> list = userService.findList(etc);
        model.addAttribute("list",list);
        model.addAttribute("etc",etc);
        return "usershow";
    }

    @RequestMapping("/toadd")
    public String toadd(){
        return "add";
    }

    @RequestMapping("/add")
    public String add(Etc etc,HttpServletRequest request){
        HttpSession session = request.getSession();
        String username = (String)session.getAttribute("username");
        etc.setName(username);
        userService.add(etc);
        return "redirect:/user/findList";
    }

    @RequestMapping("/findById")
    public String findById(Integer id,Model model){
        Etc etcs = userService.findById(id);
        model.addAttribute("etc",etcs);
        return "update";
    }

    @RequestMapping("/update")
    public String update(Etc etc,HttpServletRequest request){
        HttpSession session = request.getSession();
        String username = (String)session.getAttribute("username");
        etc.setName(username);
        userService.update(etc);
        return "redirect:/user/findList";
    }

    @RequestMapping("/delete")
    public String delete(Integer id){
        userService.delete(id);
        return "redirect:/user/findList";
    }

    @RequestMapping("/findAdmin")
    public String findAdmin(Etc etc,Model model){
        List<Etc> list = userService.findAdmin(etc);
        model.addAttribute("list",list);
        model.addAttribute("etc",etc);
        return "Adminshow";
    }
}
